home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / bisect.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  3KB  |  109 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Bisection algorithms.'''
  5.  
  6. def insort_right(a, x, lo = 0, hi = None):
  7.     '''Insert item x in list a, and keep it sorted assuming a is sorted.
  8.  
  9.     If x is already in a, insert it to the right of the rightmost x.
  10.  
  11.     Optional args lo (default 0) and hi (default len(a)) bound the
  12.     slice of a to be searched.
  13.     '''
  14.     if lo < 0:
  15.         raise ValueError('lo must be non-negative')
  16.     lo < 0
  17.     if hi is None:
  18.         hi = len(a)
  19.     
  20.     while lo < hi:
  21.         mid = (lo + hi) // 2
  22.         if x < a[mid]:
  23.             hi = mid
  24.             continue
  25.         lo = mid + 1
  26.     a.insert(lo, x)
  27.  
  28. insort = insort_right
  29.  
  30. def bisect_right(a, x, lo = 0, hi = None):
  31.     '''Return the index where to insert item x in list a, assuming a is sorted.
  32.  
  33.     The return value i is such that all e in a[:i] have e <= x, and all e in
  34.     a[i:] have e > x.  So if x already appears in the list, a.insert(x) will
  35.     insert just after the rightmost x already there.
  36.  
  37.     Optional args lo (default 0) and hi (default len(a)) bound the
  38.     slice of a to be searched.
  39.     '''
  40.     if lo < 0:
  41.         raise ValueError('lo must be non-negative')
  42.     lo < 0
  43.     if hi is None:
  44.         hi = len(a)
  45.     
  46.     while lo < hi:
  47.         mid = (lo + hi) // 2
  48.         if x < a[mid]:
  49.             hi = mid
  50.             continue
  51.         lo = mid + 1
  52.     return lo
  53.  
  54. bisect = bisect_right
  55.  
  56. def insort_left(a, x, lo = 0, hi = None):
  57.     '''Insert item x in list a, and keep it sorted assuming a is sorted.
  58.  
  59.     If x is already in a, insert it to the left of the leftmost x.
  60.  
  61.     Optional args lo (default 0) and hi (default len(a)) bound the
  62.     slice of a to be searched.
  63.     '''
  64.     if lo < 0:
  65.         raise ValueError('lo must be non-negative')
  66.     lo < 0
  67.     if hi is None:
  68.         hi = len(a)
  69.     
  70.     while lo < hi:
  71.         mid = (lo + hi) // 2
  72.         if a[mid] < x:
  73.             lo = mid + 1
  74.             continue
  75.         hi = mid
  76.     a.insert(lo, x)
  77.  
  78.  
  79. def bisect_left(a, x, lo = 0, hi = None):
  80.     '''Return the index where to insert item x in list a, assuming a is sorted.
  81.  
  82.     The return value i is such that all e in a[:i] have e < x, and all e in
  83.     a[i:] have e >= x.  So if x already appears in the list, a.insert(x) will
  84.     insert just before the leftmost x already there.
  85.  
  86.     Optional args lo (default 0) and hi (default len(a)) bound the
  87.     slice of a to be searched.
  88.     '''
  89.     if lo < 0:
  90.         raise ValueError('lo must be non-negative')
  91.     lo < 0
  92.     if hi is None:
  93.         hi = len(a)
  94.     
  95.     while lo < hi:
  96.         mid = (lo + hi) // 2
  97.         if a[mid] < x:
  98.             lo = mid + 1
  99.             continue
  100.         hi = mid
  101.     return lo
  102.  
  103.  
  104. try:
  105.     from _bisect import bisect_right, bisect_left, insort_left, insort_right, insort, bisect
  106. except ImportError:
  107.     pass
  108.  
  109.